<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input style="width: 200px;" class="filter-item" placeholder="请输入标题" v-model="searchForm.search_title">
      </el-input>
      <el-select clearable class="filter-item" style="width: 130px" v-model="searchForm.search_type" placeholder="请输入类型">
        <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item">
        </el-option>
      </el-select>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleQuery">查询</el-button>
      <el-button class="filter-item" type="danger" v-waves icon="el-icon-edit" style="margin-left: 10px;float: right;" @click="handleCreate">添加</el-button>
    </div>

    <el-table :key='tableKey' :data="pageData.datalist" v-loading="listLoading" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="时间" prop="birthday" ></el-table-column>
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="类型" prop="type"></el-table-column>
      <el-table-column label="作者" prop="author"></el-table-column>
      <el-table-column label="阅读数" prop="pageviews"></el-table-column>
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background  @current-change="handlePageNumChange"
                     :current-page="pageData.pagenum"
                     layout="total, prev, pager, next, jumper" :total="pageData.totalelements">
      </el-pagination>
    </div>

    <demo-edit ref="demoEdit" @success="handleQuery"></demo-edit>

  </div>
</template>

<script>
import demoEdit from './DemoEdit'
import demoapi from '@/api/demo'

export default {
  components: {
    demoEdit
  },
  data() {
    return {
      searchForm: {
        search_title: '',
        search_type: ''
      },
      pageData: {
        datalist: [],
        pagenum: 0,
        totalelements: 0,
        pagesize: 10
      },
      tableKey: 0,
      listLoading: true,
      statusOptions: ['published', 'draft', 'deleted']
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    /**
     * 处理查询
     */
    handleQuery(pagenum = 0, pagesize = 10) {
      this.listLoading = true
      demoapi.queryByPage(this.searchForm, pagenum, this.pageData.pagesize)
        .then(resultPage => {
          this.pageData = resultPage
          this.listLoading = false
        })
    },
    /**
     * 当前分页号变化的处理
     */
    handlePageNumChange(val) {
      this.handleQuery(val)
    },
    /**
     * 处理创建
     */
    handleCreate() {
      this.$refs['demoEdit'].handleCreate()
    },
    /**
     * 处理更新
     */
    handleUpdate(data) {
      this.$refs['demoEdit'].handleUpdate(data.id)
    },
    /**
     * 处理删除
     */
    handleDelete(row) {
      this.$confirm('确定要删除选择的记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((result) => {
        demoapi.deleteData(row.id).then(res => {
          if (res.success) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.handleQuery(this.pageData.pagenum)
          } else {
            this.$message.success(res.message || '删除失败')
          }
        })
      }, cancle => {})
    }

  }
}
</script>
